package com.iot.manager.utils;

import android.content.Context;
import android.os.Environment;
import android.util.Log;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class ExcelExporter {
    private static final String TAG = "ExcelExporter";

    public static File exportDeviceData(Context context, String deviceId, String deviceName, List<DeviceData> dataList) {
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("设备数据");

        // 创建标题样式
        CellStyle headerStyle = workbook.createCellStyle();
        headerStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
        headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        Font headerFont = workbook.createFont();
        headerFont.setBold(true);
        headerStyle.setFont(headerFont);

        // 创建标题行
        Row headerRow = sheet.createRow(0);
        String[] headers = {"时间戳", "温度(°C)", "湿度(%)"};
        for (int i = 0; i < headers.length; i++) {
            Cell cell = headerRow.createCell(i);
            cell.setCellValue(headers[i]);
            cell.setCellStyle(headerStyle);
        }

        // 添加数据
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        int rowNum = 1;
        for (DeviceData data : dataList) {
            Row row = sheet.createRow(rowNum++);
            row.createCell(0).setCellValue(dateFormat.format(new Date(data.getTimestamp())));
            row.createCell(1).setCellValue(data.getTemperature());
            row.createCell(2).setCellValue(data.getHumidity());
        }

        // 自动调整列宽
        for (int i = 0; i < headers.length; i++) {
            sheet.autoSizeColumn(i);
        }

        // 创建文件名
        String fileName = String.format("设备数据_%s_%s.xlsx", deviceName, 
            new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date()));

        // 保存文件
        File file = new File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), fileName);
        try (FileOutputStream outputStream = new FileOutputStream(file)) {
            workbook.write(outputStream);
            workbook.close();
            return file;
        } catch (IOException e) {
            Log.e(TAG, "导出Excel失败", e);
            return null;
        }
    }

    public static class DeviceData {
        private long timestamp;
        private double temperature;
        private double humidity;

        public DeviceData(long timestamp, double temperature, double humidity) {
            this.timestamp = timestamp;
            this.temperature = temperature;
            this.humidity = humidity;
        }

        public long getTimestamp() {
            return timestamp;
        }

        public double getTemperature() {
            return temperature;
        }

        public double getHumidity() {
            return humidity;
        }
    }
} 